package com.apis.javacommon.page;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

public class PageRequest
  implements Serializable
{

  private int pageNumber;
  private int pageSize;
  private String sortColumns;

  public PageRequest()
  {
    this(0, 0);
  }


  public PageRequest(int pageNumber, int pageSize) {
	  this.pageNumber = pageNumber;
	  this.pageSize = pageSize;
  }


  public PageRequest(int pageNumber, int pageSize,String sortColumns) {
    this(pageNumber, pageSize);
    this.sortColumns = sortColumns;
    
  }

  public int getPageNumber() {
    return this.pageNumber;
  }

  public void setPageNumber(int pageNumber) {
    this.pageNumber = pageNumber;
  }

  public int getPageSize() {
    return this.pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public String getSortColumns() {
    return this.sortColumns;
  }

  public void setSortColumns(String sortColumns)
  {
    checkSortColumnsSqlInjection(sortColumns);
    if ((sortColumns != null) && (sortColumns.length() > 50)) {
      throw new IllegalArgumentException("sortColumns.length() <= 50 must be true");
    }
    this.sortColumns = sortColumns;
  }

  public List<SortInfo> getSortInfos()
  {
    return Collections.unmodifiableList(SortInfo.parseSortColumns(this.sortColumns));
  }

  private void checkSortColumnsSqlInjection(String sortColumns) {
    if (sortColumns == null) return;
    if ((sortColumns.indexOf("'") >= 0) || (sortColumns.indexOf("\\") >= 0))
      throw new IllegalArgumentException("sortColumns:" + sortColumns + " has SQL Injection risk");
  }
}